package com.ruoyi.share.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import java.io.InputStream;
import org.springframework.web.multipart.MultipartFile;
import com.ruoyi.share.domain.Share;
import com.ruoyi.share.service.IShareService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 文件分享Controller
 *
 * @author huacai
 * @date 2024-07-10
 */
@RestController
@RequestMapping("/share/share")
public class ShareController extends BaseController
{
    @Autowired
    private IShareService shareService;

    /**
     * 查询文件分享列表
     */
    @PreAuthorize("@ss.hasPermi('share:share:list')")
    @GetMapping("/list")
    public TableDataInfo list(Share share)
    {
        startPage();
        List<Share> list = shareService.selectShareList(share);
        return getDataTable(list);
    }

    /**
     * 导出文件分享列表
     */
    @PreAuthorize("@ss.hasPermi('share:share:export')")
    @Log(title = "文件分享", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Share share)
    {
        List<Share> list = shareService.selectShareList(share);
        ExcelUtil<Share> util = new ExcelUtil<Share>(Share.class);
        util.exportExcel(response, list, "文件分享数据");
    }

    /**
     * 下载模板
     */
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response)
    {
        ExcelUtil<Share> util = new ExcelUtil<Share>(Share.class);
        util.importTemplateExcel(response, "文件分享数据");
    }

    /**
     * 导入数据
     */
    @Log(title = "文件分享", businessType = BusinessType.IMPORT)
    @PreAuthorize("@ss.hasPermi('share:share:import')")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file) throws Exception
    {
        ExcelUtil<Share> util = new ExcelUtil<Share>(Share.class);
        InputStream inputStream = file.getInputStream();
        List<Share> list = util.importExcel(inputStream );
        inputStream.close();
        int count = shareService.batchInsertShare(list);
        return AjaxResult.success("导入成功" + count + "条信息！");
    }

    /**
     * 获取文件分享详细信息
     */
    @PreAuthorize("@ss.hasPermi('share:share:query')")
    @GetMapping(value = "/{shareId}")
    public AjaxResult getInfo(@PathVariable("shareId") Integer shareId)
    {
        return success(shareService.selectShareByShareId(shareId));
    }

    /**
     * 新增文件分享
     */
    @PreAuthorize("@ss.hasPermi('share:share:add')")
    @Log(title = "文件分享", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Share share)
    {
        return toAjax(shareService.insertShare(share));
    }

    /**
     * 修改文件分享
     */
    @PreAuthorize("@ss.hasPermi('share:share:edit')")
    @Log(title = "文件分享", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Share share)
    {
        return toAjax(shareService.updateShare(share));
    }

    /**
     * 删除文件分享
     */
    @PreAuthorize("@ss.hasPermi('share:share:remove')")
    @Log(title = "文件分享", businessType = BusinessType.DELETE)
	@DeleteMapping("/{shareIds}")
    public AjaxResult remove(@PathVariable Integer[] shareIds)
    {
        return toAjax(shareService.deleteShareByShareIds(shareIds));
    }
}
